/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

var MyDesktop={};
//joshua.Reloj= new Ext.util.TaskRunner();
//joshua.Stop=false;//variable para parar el reloj
// Sample desktop configuration


	    
		
joshua.Escritorio=function(){
	//usuarios
	MyDesktop.UsuariosWin = Ext.extend(Ext.app.Module, {
	    id:'usuarios-win',
	    init : function(){
	        this.launcher = {
	            text: 'Usuarios',
	            iconCls:'usuario',
	            handler : this.createWindow,
	            scope: this
	        }
	    },
	
	    createWindow : function(){
	        var desktop = this.app.getDesktop();
	        joshua.usuarios.win = desktop.getWindow('usuarios-win');
	        if(!joshua.usuarios.win){
                    var g=joshua.usuarios.mostrarInterfaz();
	            joshua.usuarios.win = desktop.createWindow({
	                id: 'usuarios-win',
	                title:'Usuarios',
	                width:740,
	                height:480,
	                iconCls: 'usuario',
	                shim:false,
	                animCollapse:false,
	                constrainHeader:true,
	                layout: 'fit',
	                items: g              	
	            });
	        }
	        joshua.usuarios.win.show();
	    }
	});
	//envios
	MyDesktop.EnviosWin = Ext.extend(Ext.app.Module, {
	    id:'envios-win',
	    init : function(){
	        this.launcher = {
	            text: 'Envios',
	            iconCls:'envio',
	            handler : this.createWindow,
	            scope: this
	        }
	    },
	
	    createWindow : function(){
	        var desktop = this.app.getDesktop();
	        joshua.wEnvios = desktop.getWindow('envios-win');
	        if(!joshua.wEnvios){
				//enmascaramos para la ventana de envios que tarda en salir
			//	Ext.getBody().mask('<img src="./img/loading2.gif" style="width:16px;height:16px;" align="rigth"> espere ...');
				var layout=joshua.envios.mostrarInterfaz();
	            joshua.wEnvios = desktop.createWindow({
	                id: 'envios-win',
	                title:'Envios',
	                //width:740,
	                //height:480,
	                iconCls: 'envio',
	                shim:false,
			width: 950,
			height: 550,
	                animCollapse:false,
	                constrainHeader:true,
	                layout: 'fit',	
	                items: layout
	            });
	        }
//	        win.
	        joshua.wEnvios.show();
              //  joshua.wEnvios.maximize();
	        //escondemos el toolbar que muestra las cantidades
	  //      if((joshua.Nivel=='Taquillero'))
	   //     		win.items.items[0].gridEnvios.getBottomToolbar().hide();
	    }
	});

	MyDesktop.Clientes = Ext.extend(Ext.app.Module, {
	    id:'clientes-win',
	    init : function(){
	        this.launcher = {
	            text: 'Clientes',
	            iconCls:'grupo',
	            handler : this.createWindow,
	            scope: this
	        }
	    },
	
	    createWindow : function(){
	        var desktop = this.app.getDesktop();
	        var win = desktop.getWindow('clientes-win');
	        if(!win){
				joshua.clientes.grid = new  joshua.clientes.Grid({});
	            win = desktop.createWindow({
	                id: 'clientes-win',
	                title:'Clientes',
	                width:740,
	                height:480,
	                iconCls: 'grupo',
	                shim:false,
	                animCollapse:false,
	                constrainHeader:true,
	                layout: 'fit',
	                items: joshua.clientes.grid              	
	            });
	        }
	        win.show();
	    }
	});


	MyDesktop.Embarques = Ext.extend(Ext.app.Module, {
	    id:'embarques-win',
	    init : function(){
	        this.launcher = {
	            text: 'Embarques',
	            iconCls:'avionicono1',
	            handler : this.createWindow,
	            scope: this
	        }
	    },
	
	    createWindow : function(){
	        var desktop = this.app.getDesktop();
	        joshua.winEm = desktop.getWindow('embarques-win');
	        if(!joshua.winEm){
				var j= joshua.embarques.mostrarInterfaz();
	            joshua.winEm = desktop.createWindow({
	                id: 'embarques-win',
	                title:'Embarques',
	                width:740,
	                height:480,
	                iconCls: 'avionicono1',
	                shim:false,
	                animCollapse:false,
	                constrainHeader:true,
	                layout: 'fit',
	                items: j    	
	            });
	        }
	        joshua.winEm.show();
	    }
	});


	MyDesktop.Sucursales = Ext.extend(Ext.app.Module, {
	    id:'sucursales-win',
	    init : function(){
	        this.launcher = {
	            text: 'Sucursales',
	            iconCls:'sucursal',
	            handler : this.createWindow,
	            scope: this
	        }
	    },
	
	    createWindow : function(){
	        var desktop = this.app.getDesktop();
	        var win = desktop.getWindow('sucursales-win');
	        if(!win){
				var j= joshua.sucursales.mostrarInterfaz();
	            win = desktop.createWindow({
	                id: 'sucursales-win',
	                title:'Sucursales',
	                width:740,
	                height:480,
	                iconCls: 'sucursal',
	                shim:false,
	                animCollapse:false,
	                constrainHeader:true,
	                layout: 'fit',
	                items: j    	
	            });
	        }
	        win.show();
	    }
	});


	//tema
	MyDesktop.Tema = Ext.extend(Ext.app.Module, {
	    id:'tema-win',
	    init : function(){
	        this.launcher = {
	            text: 'Elegir tema',
	            iconCls:'paint',
	            handler : this.createWindow,
	            scope: this
	        }
	    },

		createWindow :function(){
	        var desktop = this.app.getDesktop();
	        var win = desktop.getWindow('tema-win');
	        if (!win) {
				win = desktop.createWindow({
					id: 'tema-win',
					title: 'Elegir tema',
					iconCls: 'paint',
					shim: false,
					width: 200,
					animCollapse: false,
					constrainHeader: true,
					layout: 'fit',
					items: [ new Ext.form.ComboBox({
						id: 'txt_tema_',
						store: [['ext3/resources/css/xtheme-blue.css', 'BlueTheme'], ['ext3/resources/css/BlackTheme/css/xtheme-black.css', 'BlackTheme'], ['ext3/resources/css/DarkGrayTheme/css/xtheme-darkgray.css', 'DarkGrayTheme'], ['ext3/resources/css/OliveTheme/css/xtheme-olive.css', 'OliveTheme'], ['ext3/resources/css/PurpleTheme/css/xtheme-purple.css', 'PurpleTheme']],
						emptyText: 'Elije un nuevo tema...',
						width: 100,
						//value: 0,
						typeAhead: true,
						triggerAction: 'all',
						selectOnFocus: true,
						allowBlank: false,
						editable: false
					})],
					buttons: [new Ext.Button({
						text: '<b>Aplicar</b>',
						iconCls: 'paintbrush',
						scope: this,
						handler: function(){
							var v_theme = Ext.getCmp('txt_tema_').getValue();
							Ext.util.CSS.swapStyleSheet('theme', v_theme);
							
							var conn=new Ext.data.Connection();
							conn.request({
								url:'Servicios/s.php', params: 'cmd=cTema&className=Session&t='+v_theme,
								scope:this,
								success:function(resp,opt){		
									
								}
							});
						}
					}), {
						text: '<b>Cerrar</b>',
						scope: this,
						iconCls: 'cerrar',
						handler: function(){
							win.close();
						}
					}]
				});
			}
	        win.show();       
	    }
	});


	MyDesktop.TipoCambio = Ext.extend(Ext.app.Module, {
	    id:'tipocambio-win',
	    init : function(){
	        this.launcher = {
	            text: 'Tipo de cambio',
	            iconCls:'money',
	            handler : this.createWindow,
	            scope: this
	        }
	    },

		createWindow :function(){
	        var desktop = this.app.getDesktop();
	        var win = desktop.getWindow('tipocambio-win');
	        if (!win) {
				win = desktop.createWindow({
					id: 'tipocambio-win',
					title: 'Tipo de cambio de hoy',
					iconCls: 'money',
					shim: false,
					width: 200,
					animCollapse: false,
					constrainHeader: true,
					layout: 'fit',
                                        listeners:{
                                            show: function(){
                                                this.el.mask();
                                                var conn=new Ext.data.Connection();
                                                conn.request({
                                                    url:'Servicios/s.php', params: 'cmd=cargar&className=TipoCambio',
                                                    scope:this,
                                                    success:function(resp,opt){
                                                        Ext.getCmp('nuevotp').setValue(resp.responseText);
                                                        this.el.unmask();
                                                    }
                                                });
                                            }
                                        },
					items: [ {
                                                            xtype:'textfield',
                                                            fieldLabel: 'Peso',
                                                            id: 'nuevotp',
                                                            width:100,
                                                            maxLength:5,
                                                            allowBlank:false
				    	            }],
					buttons: [new Ext.Button({
						text: '<b>Aplicar</b>',
						iconCls: 'aceptar',
						scope: this,
						handler: function(){
                                                    var conn=new Ext.data.Connection();
                                                    conn.request({
                                                        url:'Servicios/s.php', params: 'cmd=editar&className=TipoCambio&nuevotp='+Ext.getCmp('nuevotp').getValue(),
                                                        scope:this,
                                                        success:function(resp,opt){
                                                            win.close();
                                                            joshua.funciones.alert('aceptar','Guardado!',"Nuevo tipo de cambio aplicado!");
                                                        }
                                                    });
						}
					}), {
						text: '<b>Cerrar</b>',
						scope: this,
						iconCls: 'cerrar',
						handler: function(){
							win.close();
						}
					}]
				});
			}
	        win.show();
	    }
	});


	MyDesktop = new Ext.app.App({
		init :function(){
			Ext.QuickTips.init();
			//this.checarSession();
		},
	
		getModules : function(){
                    joshua.winEnvios=new MyDesktop.EnviosWin();
					joshua.winEmbarques=new MyDesktop.Embarques();
			/*var def=[
					new MyDesktop.EnviosWin(),
					new MyDesktop.Clientes(),
					new MyDesktop.Embarques(),
					new MyDesktop.Sucursales(),
					new MyDesktop.UsuariosWin()
				];			
                         */
                        var def= eval(joshua.wins);
			def.push(new MyDesktop.Tema());
			return def;			
		},
	
	    // config for the start menu
	    getStartConfig : function(){
	        return {
	            title: joshua.InfoUser,
	            iconCls: 'usuario',
	            toolItems: [{
	                text:'Salir',
	                iconCls:'logout',
	                scope:this,
	                handler:joshua.funciones.CerrarSesion
	            }
				]
	        };
	    }
	});
}
joshua.IniciarEscritorio = function() {	 	
	Ext.getBody().update(joshua.HtmlDesktop).fadeIn({
							    endOpacity: 1,
							    easing: 'easeOut',
							    duration: .5
							});
	joshua.Escritorio();

	new Ext.DatePicker({
		renderTo:'esc_calendar'
	});
	
	
	//escribir el reloj
	var so = new SWFObject("reloj.swf", "header", "180", "88", "8", "#FF6600");
	so.addVariable("flashVarText", "this is passed in via FlashVars for example only");
	so.addParam("menu", "false");
	so.addParam("wmode", "transparent");
	so.write("esc_reloj");
	
	
	var ve_foto=new joshua.util.FileForm();
	
	new Ext.Panel({
		html:'<img id="avatar_usr" src="'+joshua.foto+'" width="100">',
		width:105,
		frame:true,
		border:true,
		renderTo:'esc_avatar',
		buttonAlign:'right',
		tbar:['<b>'+joshua.login+'</b>'],
		bbar:['->',{
			iconCls:'imagen_agregar',
			tooltip:'Presione para cambiar su imagen para mostrar',
			handler:function (){
				ve_foto.show();
			},
			hideLabel :true
		}]
		
	});
	
}       

